
package com.crowdfunding.circle.sensitiveword.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.crowdfunding.circle.common.controller.BaseController;
import com.crowdfunding.circle.common.param.SessionUserInfo;
import com.crowdfunding.circle.sensitiveword.SensitiveWordUtils;
import com.crowdfunding.circle.sensitiveword.entity.SensitiveWordVO;
import com.crowdfunding.circle.sensitiveword.param.SensitiveWordParam;
import com.crowdfunding.circle.sensitiveword.service.SensitiveWordService;

/**
 * 敏感词设置Controller
 *
 * @author 朱国军
 * @since 1.0
 * @version 2015年12月27日 朱国军
 */
@Controller
public class SensitiveWordController extends BaseController {
    
    private Logger logger = LoggerFactory.getLogger(SensitiveWordController.class);
    
    @Autowired
    private SensitiveWordService sensitiveWordService;
    
    private final static String LIST_PAGE = "/admin/sensitiveWord_list";
    
    private final static String UPDATE_PAGE = "/admin/sensitiveWord_update";

    @RequestMapping(value = "/admin/sensitiveWord/toInsert")
    public ModelAndView intoInsert() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        SensitiveWordVO sensitiveWordVO = new SensitiveWordVO();
        resultMap.put("sensitiveWordVO", sensitiveWordVO);
        return new ModelAndView(UPDATE_PAGE, resultMap);
    }

    @RequestMapping(value = "/admin/sensitiveWord/toUpdate/{id}")
    public ModelAndView intoUpdate(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        SensitiveWordVO sensitiveWordVO = sensitiveWordService.findById(id);
        resultMap.put("sensitiveWordVO", sensitiveWordVO);
        return new ModelAndView(UPDATE_PAGE, resultMap);
    }

    @RequestMapping(value = "/admin/sensitiveWord/update", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String update(SensitiveWordVO sensitiveWordVO) {
        SessionUserInfo seUser = (SessionUserInfo) session.getAttribute("sessionUserInfo");
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {

            if (SensitiveWordUtils.isContaintSensitiveWord(sensitiveWordVO.getSensitiveWord(), 1)) {
                resultMap.put("result", false);
                resultMap.put("message", "该敏感词已存在");
                return JSONObject.toJSONString(resultMap);
            }

            Integer affectCount = 0;
            if (sensitiveWordVO != null && StringUtils.isNotBlank(sensitiveWordVO.getWordId())) {
                affectCount = sensitiveWordService.update(sensitiveWordVO);
            } else if (sensitiveWordVO != null) {
                sensitiveWordVO.setWordId(UUID.randomUUID().toString());
                sensitiveWordVO.setPublishUser(seUser.getUserId());
                sensitiveWordVO.setDataStatus(0);
                affectCount = sensitiveWordService.insert(sensitiveWordVO);
            }
            if (affectCount != null && affectCount == 1) {
            	SensitiveWordUtils.initSensitiveWord();
                resultMap.put("result", true);
                resultMap.put("message", "新增或修改敏感词设置成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "新增或修改敏感词设置失败");
            }
        } catch (Exception e) {
            logger.error("新增或修改敏感词设置出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "新增或修改敏感词设置出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }

    @RequestMapping(value = "/admin/sensitiveWord/check", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String check(String checkWord) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        if (StringUtils.isBlank(checkWord)) {
            resultMap.put("result", false);
            resultMap.put("message", "要检查的文本为空！");
            return JSONObject.toJSONString(resultMap);
        } else {
            Set<String> sensitiveWords = SensitiveWordUtils.getSensitiveWord(checkWord, 1);

            if (sensitiveWords == null || sensitiveWords.isEmpty()) {
                resultMap.put("result", false);
                resultMap.put("message", "未检测到敏感词");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "包含敏感词:" + sensitiveWords);
            }
            return JSONObject.toJSONString(resultMap);
        }
    }
    
    @RequestMapping(value = "/admin/sensitiveWord/delete/{id}", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String delete(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Integer deleteCount = sensitiveWordService.delete(id);
            if (deleteCount != null && deleteCount == 1) {
            	SensitiveWordUtils.initSensitiveWord();
                resultMap.put("result", true);
                resultMap.put("message", "删除敏感词设置成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "删除敏感词设置失败");
            }
        } catch (Exception e) {
            logger.error("删除敏感词设置出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "删除敏感词设置出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }
    
    @RequestMapping(value = "/admin/sensitiveWord/list")
    public ModelAndView queryList(SensitiveWordParam sensitiveWordParam) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<SensitiveWordVO> dataList = sensitiveWordService.queryByPage(sensitiveWordParam);
        resultMap.put("dataList", dataList);
        resultMap.put("paramMap", sensitiveWordParam);
        return new ModelAndView(LIST_PAGE, resultMap);
    }
}
